package com.tengxing.e1.FactoryTest;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.maiarstar.e101.protocol.cerebellum.api.exception.ProtocolException;
import com.maiarstar.e101.protocol.cerebellum.api.module.ICerebellumProtocolGetter;
import com.maiarstar.e101.protocol.cerebellum.sdk.CerebellumProtocolSdkManager;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Ver_Test_Activity extends AppCompatActivity {
    private static final String TAG = "Ver_Test_Activity";
    private CerebellumProtocolSdkManager cerebellumProtocolSdkManager;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    Button btn_Ver_Pass, btn_Ver_Fail;

    TextView tV_HWV, tV_SWV, tV_SWV1, tV_URV, tV_OSV, eT_sn, eT_imei1, eT_imei2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_ver_test);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        sharedPreferences = getSharedPreferences("Test_Result", MODE_PRIVATE);
        initCerebellumManager();
        Factory_Ver_BTN();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Factory_Ver_TV();
                Toast.makeText(Ver_Test_Activity.this, "delay ms", Toast.LENGTH_SHORT).show();
                Log.d(TAG, "TaskRUN:in");
            }
        }, 1000);
    }
    private void initCerebellumManager() {
        CerebellumProtocolSdkManager sdkProtocolManager = new CerebellumProtocolSdkManager(this);
        CerebellumProtocolSdkManager.SdkParams sdkParams = CerebellumProtocolSdkManager.getDeafultSdkParams();
        try {
            sdkProtocolManager.init(sdkParams);
        } catch (ProtocolException e) {
            Log.w(TAG, e.getMessage());
        }
        this.cerebellumProtocolSdkManager = sdkProtocolManager;
    }

    private void Factory_Ver_BTN() {
        btn_Ver_Pass = findViewById(R.id.btn_Ver_Pass);
        btn_Ver_Pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor = sharedPreferences.edit();
                editor = editor.putString("Ver", "通过");
                if (editor.commit()) {
                    Toast.makeText(Ver_Test_Activity.this, "测试结果保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Ver_Test_Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(Ver_Test_Activity.this, "测试结果保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_Ver_Fail = findViewById(R.id.btn_Ver_Fail);
        btn_Ver_Fail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor = sharedPreferences.edit();
                editor = editor.putString("Ver", "未通过");
                if (editor.commit()) {
                    Toast.makeText(Ver_Test_Activity.this, "测试结果保存成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Ver_Test_Activity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(Ver_Test_Activity.this, "测试结果保存失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void Factory_Ver_TV(){
        String hwv_str = "SRB硬件版本：";
        hwv_str = hwv_str.concat(GetHWV());
        tV_HWV = findViewById(R.id.tV_HWV);
        tV_HWV.setText(hwv_str);

        String swv_str = "SRB软件版本：";
        swv_str = swv_str.concat(GetSWV());
        tV_SWV = findViewById(R.id.tV_SWV);
        tV_SWV.setText(swv_str);

        String swv_str1 = "EXB软件版本：";
        swv_str1 = swv_str1.concat(GetSWV1());
        tV_SWV1 = findViewById(R.id.tV_SWV1);
        tV_SWV1.setText(swv_str1);

        String urv_str = "SRB Usart版本：";
        urv_str = urv_str.concat(GetURV());
        tV_URV = findViewById(R.id.tV_URV);
        tV_URV.setText(urv_str);

        String system_ver = "SCB软件版本：";
        system_ver = system_ver.concat(read_System_Ver());
        tV_OSV = findViewById(R.id.tV_OSV);
        tV_OSV.setText(system_ver);

        eT_sn = findViewById(R.id.eT_sn);
        try{
            eT_sn.setText(Build.getSerial());
        }catch (Exception e){
            Log.e(TAG,"S/N:"+e);
        }

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        eT_imei1 = findViewById(R.id.eT_imei1);
        eT_imei2 = findViewById(R.id.eT_imei2);
        try{
            eT_imei1.setText(telephonyManager.getImei(0));
            eT_imei2.setText(telephonyManager.getImei(1));
        }catch (Exception e){
            Log.e("MainActivity.java","IMEI:"+e);
        }
    }

    private String GetHWV() throws ProtocolException {
        try {
            ICerebellumProtocolGetter getter = this.cerebellumProtocolSdkManager.getCerebellumProtocolGetter();
            return getter.getHWVInfo().getVer();
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        }
        return "获取失败";
    }

    private String GetURV() throws ProtocolException {
        try {
            ICerebellumProtocolGetter getter = this.cerebellumProtocolSdkManager.getCerebellumProtocolGetter();
            return getter.getURVInfo().getVer();
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        }
        return "获取失败";
    }

    private String GetSWV() throws ProtocolException {
        try {
            ICerebellumProtocolGetter getter = this.cerebellumProtocolSdkManager.getCerebellumProtocolGetter();
            return getter.getSWVInfo().getVer();
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        }
        return "获取失败";
    }

    private String GetSWV1() throws ProtocolException {
        try {
            ICerebellumProtocolGetter getter = this.cerebellumProtocolSdkManager.getCerebellumProtocolGetter();
            return getter.getSWVInfo1().getVer();
        } catch (Exception e) {
            Log.w(TAG, e.getMessage());
        }
        return "获取失败";
    }

    private String read_System_Ver(){
        return getSystemProperty("ro.build.urv.id");
    }

    private String getSystemProperty(String key){
        try{
            ProcessBuilder builder = new ProcessBuilder("getprop", key);
            Process process = builder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String value = reader.readLine();
            reader.close();
            return value;
        }catch (Exception e){
            Log.e(TAG,"prop:"+e);
            return null;
        }
    }


    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Ver_Test_Activity.this, MainActivity.class);
        startActivity(intent);
        finish();
        Toast.makeText(Ver_Test_Activity.this, "直接退出不会保存结果", Toast.LENGTH_SHORT).show();
        super.onBackPressed();
    }

}